package com.bjpowernode.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bjpowernode.shop.base.BaseMember;
import com.bjpowernode.shop.domain.User;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/p/user")
public class UserController extends BaseMember {

    @PutMapping("/setUserInfo")
    public ResponseEntity<Boolean> setUserInfo(@RequestBody User user) {
        // 更新前台用户的昵称、头像地址、性别，根据openid(security中的username)
        user.setUserId(getStringUserId()).setSex(user.getSex().equals("0") ? "M" : "F");

        return ResponseEntity.ok(userService.update(
                user,
                new LambdaQueryWrapper<User>()
                .eq(ObjectUtils.isNotEmpty(user),User::getUserId,user.getUserId())
        ));
    }
}
